//
//  MovieViewController.m
//  Movie
//
//  Created by mycomputer on 15/7/17.
//  Copyright (c) 2015年 杨韦智. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieModel.h"
#import "MovieTableViewCell.h"

@interface MovieViewController ()

@end

@implementation MovieViewController

#pragma mark - MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"电影";
    }
    return  self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self _createNav];
    
    [self _createMovieTableView];
    
    [self _createPoseterView];
    
    [self _loadData];
    UINib *nib = [UINib nibWithNibName:@"MovieTableViewCell" bundle:[NSBundle mainBundle]];
    [_movieTableView registerNib:nib forCellReuseIdentifier:@"cell"];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - CreateSubViews
- (void)_createNav {
    UIView *flipView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    
    UIButton *btn1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn1 setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    [btn1 sizeToFit];
    btn1.tag = 1;
    [btn1 addTarget:self action:@selector(flipBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [flipView addSubview:btn1];
    [btn2 setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    [btn2 sizeToFit];
    btn2.tag = 2;
    [btn2 addTarget:self action:@selector(flipBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    btn2.hidden = YES;
    [flipView addSubview:btn2];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:flipView];
    self.navigationItem.rightBarButtonItem = item;
}

- (void)_createPoseterView {
    _posterView = [[UIView alloc] initWithFrame:self.view.bounds];
    _posterView.hidden = YES;
    [self.view addSubview:_posterView];
}

- (void)_createMovieTableView {
    _movieTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _movieTableView.dataSource = self;
    _movieTableView.delegate = self;
    //_movieTableView.hidden = YES;
    [self.view addSubview:_movieTableView];
}

#pragma mark - LoadData
- (void)_loadData {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"us_box.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    _movieModelArray = [[NSMutableArray alloc] init];
    NSArray *subjects = [dataDic objectForKey:@"subjects"];
    for (NSDictionary *dic in subjects) {
        NSDictionary *subject = [dic objectForKey:@"subject"];
        MovieModel *model = [[MovieModel alloc] init];
        model.year = [subject objectForKey:@"year"];
        model.title = [subject objectForKey:@"title"];
        model.images = [subject objectForKey:@"images"];
        NSNumber *number = [[subject objectForKey:@"rating"] objectForKey:@"average"];
        model.average = [number floatValue];
        [_movieModelArray addObject:model];
    }
    
}

#pragma mark - Action
- (void)flipBtnAction:(UIButton *)button {
    UIView *flipView = self.navigationItem.rightBarButtonItem.customView;
    
    UIButton *btn1 = (UIButton *)[flipView viewWithTag:1];
    UIButton *btn2 = (UIButton *)[flipView viewWithTag:2];
    
    BOOL isLeft = btn2.hidden;
    
    [self _transitionWithView:flipView isLeft:isLeft];
    [self _transitionWithView:self.view isLeft:isLeft];
    
    btn1.hidden = !btn1.hidden;
    btn2.hidden = !btn2.hidden;
    _posterView.hidden = !_posterView.hidden;
    _movieTableView.hidden = !_movieTableView.hidden;
    
}

- (void)_transitionWithView:(UIView *)flipView isLeft:(BOOL)isLeft {
    UIViewAnimationOptions option = isLeft?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;
    [UIView transitionWithView:flipView duration:.3 options:option animations:nil completion:nil];
    //[UIView transitionWithView:self.view duration:.3 options:option animations:nil completion:nil];
}

#pragma mask - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _movieModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    MovieModel *model = _movieModelArray[indexPath.row];
    cell.model = model;
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
